package com.icss.ah.fullReduce.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FullReduceService {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 满减活动列表查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getFullReduceList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullReduceBean.getFullReduceList",map);
			infolog.info("FullReduceService|getFullReduceList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|getFullReduceList", e);
		}
		return list;
	}
	
	/**
	 * 满减活动统计
	 * @param map
	 * @return
	 */
	public Long getFullReduceListCount(Map<String, String> map){
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("FullReduceBean.getFullReduceListCount",map);
			infolog.info("FullReduceService|getFullReduceListCount|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|getFullReduceListCount", e);
		}
		return count;
	}
	
	/**
	 * 满减活动保存
	 * @param map
	 */
	public void saveInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.insert("FullReduceBean.saveInfo",map);
			infolog.info("FullReduceService|saveInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|saveInfo", e);
		}
	}
	
	/**
	 * 满减活动编辑
	 * @param map
	 */
	public void updateInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.update("FullReduceBean.updateInfo",map);
			infolog.info("FullReduceService|updateInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|updateInfo", e);
		}
	}
	
	/**
	 * 满减活动状态启用/禁用
	 * @param rtsId
	 */
	public void updateStatus(String rtsId){
		try {
			sqlSessionTemplate.update("FullReduceBean.updateStatus",rtsId);
			infolog.info("FullReduceService|updateStatus");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|updateStatus", e);
		}
	}
	
	/**
	 * 满减活动详情
	 * @param rtsId
	 * @return
	 */
	public Map<String, Object> getFullReduceInfo(String rdeId){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("FullReduceBean.getFullReduceInfo",rdeId);
			infolog.info("FullReduceService|getFullReduceInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|getFullReduceInfo", e);
		}
		return info;
	}

	/**
	 * 满减活动保存
	 * @param map
	 */
	public void saveFullReduceInfo(Map<String, String> map,List<Map<String, String>> list){
		sqlSessionTemplate.insert("FullReduceBean.saveFullReduceInfo",map);
		if(list.size() > 0)
			sqlSessionTemplate.insert("FullReduceBean.saveProInfo",list);
	}
	
	/**
	 * 满减活动编辑
	 * @param map
	 */
	public void updateFullReduceInfo(Map<String, String> map,List<Map<String, String>> list){
		sqlSessionTemplate.insert("FullReduceBean.updateFullReduceInfo",map);
		sqlSessionTemplate.delete("FullReduceBean.delFullReduceProduct",map);
		if(list.size() > 0)
			sqlSessionTemplate.insert("FullReduceBean.saveProInfo",list);
	}
	
	/**
	 * 商品查询
	 */
	public List<Map<String, Object>> getProductList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullReduceBean.getProductList",map);
			infolog.info("FullReduceService|getProductList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|getProductList", e);
		}
		return list;
	}
	/**
	 * 满减活动详情
	 * @param rtsId
	 * @return
	 */
	public Map<String, Object> getFullReduceInf(String rdeId){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("FullReduceBean.getFullReduceInf",rdeId);
			infolog.info("FullReduceService|getFullReduceInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|getFullReduceInfo", e);
		}
		return info;
	}
	
	public List<Map<String, Object>> getProductTypeList(Map<String, String> map){
		List<Map<String, Object>> list = sqlSessionTemplate.selectList("FullReduceBean.getProductTypeList",map);
		return list == null ? new ArrayList<Map<String,Object>>() : list;
	}
	
	/**
	 * 删除满减信息
	 * @param rdeId
	 * @return
	 */
	public String delInfo(String rdeId){
		String result = "f";
		try {
			sqlSessionTemplate.update("FullReduceBean.upInfo",rdeId);
			result = "t";
			infolog.info("FullReduceService|delInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceService|delInfo", e);
		}
		return result;
	}

}
